package csvm_v3.components;

import csvm_v1.products.CocaCola;
import csvm_v1.products.Fanta;
import csvm_v1.products.Product;
import csvm_v1.products.Sprite;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * DumpFileWriter menuliskan data vending machine ke file dump
 *
 * @author Muhmammad Anis
 */
public class DumpFileWriter {

    /**
     * reference ke stok koin
     */
    private CoinContainer cStock;
    /**
     * referensi ke stok produk
     */
    private ProductContainer pStock;
    /**
     * referensi ke product selector
     */
    private ProductSelector pSelector;

    /**
     * konstruktor DumpFileWriter
     *
     * @param coinStock stok koin
     * @param productStock stok produk
     * @param productSelector product selector
     */
    public DumpFileWriter(CoinContainer coinStock, ProductContainer productStock, ProductSelector productSelector) {
        this.cStock = coinStock;
        this.pStock = productStock;
        this.pSelector = productSelector;
    }

    /**
     * menuliskan dump file
     * @param fileName nama file
     */
    public void exportDump(String fileName) {
        FileWriter fw = null;
        try {
            HashMap<Integer, Product> catalog = pSelector.getCatalog();
            HashMap<Product, Integer> productStock = pStock.getProductStock();
            File file = new File(fileName);
            fw = new FileWriter(file.getAbsoluteFile());
            try (BufferedWriter bw = new BufferedWriter(fw)) {
                bw.write("#products\n");
                bw.write(catalog.size() + "\n");
                // menuliskan data produk
                Iterator it = catalog.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry pairs = (Map.Entry) it.next();
                    Product produk = (Product) pairs.getValue();
                    Integer jumlah = (Integer) productStock.get(produk);
                    // format line untuk fixed product : <nama : {CocaCola, Fanta, Sprite}> <jumlah>
                    if (produk instanceof CocaCola) {
                        bw.write("CocaCola " + jumlah + "\n");
                    } else if (produk instanceof Fanta) {
                        bw.write("Fanta " + jumlah + "\n");
                    } else if (produk instanceof Sprite) {
                        bw.write("Sprite " + jumlah + "\n");
                    } else {
                        // format line untuk custom product : <nama> <select index> <harga> <jumlah>
                        bw.write(produk.getProductName() + " " + produk.getSelectIndex() + " " + produk.getPrice() + " " + jumlah + "\n");
                    }
                }
                // menuliskan header coin
                bw.write("#coins\n");
                bw.write("Dime " + cStock.getDimeCount() + "\n");
                bw.write("Nickel " + cStock.getNickelCount() + "\n");
                bw.write("Quarter " + cStock.getQuarterCount() + "\n");
            }
        } catch (IOException ex) {
            Logger.getLogger(DumpFileWriter.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fw.close();
            } catch (IOException ex) {
                Logger.getLogger(DumpFileWriter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
